package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.Field;
import com.portal.logic.SerializationUtil;

public class AddCustomerScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private JTextField txtAge;
	private JTextField txtPhone;
	private JTextField txtRating;
	private JTextField txtResidentialArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddCustomerScr frame = new AddCustomerScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddCustomerScr() {
		setTitle("Add Customer");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 482, 327);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblName = new JLabel("Name");
		lblName.setBounds(52, 52, 56, 16);
		contentPane.add(lblName);

		JLabel lblAge = new JLabel("Age");
		lblAge.setBounds(52, 81, 56, 16);
		contentPane.add(lblAge);

		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(52, 139, 56, 16);
		contentPane.add(lblPhone);

		JLabel lblRating = new JLabel("Rating (From 1 to 10)");
		lblRating.setBounds(52, 168, 130, 16);
		contentPane.add(lblRating);

		JLabel lblResidentialArea = new JLabel("Residential Area");
		lblResidentialArea.setBounds(52, 110, 100, 16);
		contentPane.add(lblResidentialArea);

		txtName = new JTextField();
		txtName.setBounds(187, 49, 116, 22);
		contentPane.add(txtName);
		txtName.setColumns(10);

		txtAge = new JTextField();
		txtAge.setBounds(187, 78, 116, 22);
		contentPane.add(txtAge);
		txtAge.setColumns(10);

		//TODO in future create JComboBox for Residential Area
		//JComboBox cmbResidentialArea = new JComboBox();
		//cmbResidentialArea.setBounds(187, 107, 116, 22);
		//contentPane.add(cmbResidentialArea);
		txtResidentialArea = new JTextField();
		txtResidentialArea.setBounds(187, 107, 116, 22);
		contentPane.add(txtResidentialArea);
		txtResidentialArea.setColumns(10);

		txtPhone = new JTextField();
		txtPhone.setBounds(187, 136, 116, 22);
		contentPane.add(txtPhone);
		txtPhone.setColumns(10);

		txtRating = new JTextField();
		txtRating.setBounds(187, 165, 116, 22);
		contentPane.add(txtRating);
		txtRating.setColumns(10);

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});

		btnBack.setBounds(52, 225, 97, 25);
		contentPane.add(btnBack);

		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				String name = txtName.getText();
				String age = txtAge.getText();
				String residentialArea = txtResidentialArea.getText();
				String phone = txtPhone.getText();
				String rating = txtRating.getText();
				if(name == null || ("".equals(name)) || phone == null || ("".equals(phone))){
					JOptionPane.showMessageDialog(null, "Name and Phone of new Customer can not be empty.");
				}else{
					CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection()) ;
					Customer customer = customerDAO.selectByName(name);
					if(name.equalsIgnoreCase(customer.getName())){
						JOptionPane.showMessageDialog(null, "Such Customer already exists.");
					}else{
						int maxFieldId = customerDAO.selectMaxId();
						System.out.println(maxFieldId);
						Customer addedCustomer = new Customer(/*maxFieldId+1, name*/);
						addedCustomer.setId(maxFieldId+1);
						addedCustomer.setName(name);
						addedCustomer.setAge(Integer.parseInt(age));
						addedCustomer.setResidentialArea(residentialArea);
						addedCustomer.setPhone(phone);
						addedCustomer.setRating(Integer.parseInt(rating));

						try {
							SerializationUtil.serialize(addedCustomer, "c:\\customer.out");
						} catch (IOException e) {
							e.printStackTrace();
						}

						customerDAO.insert();
						JOptionPane.showMessageDialog(null, "New Customer was added!");
					}
				}
			}
		});
		btnSubmit.setBounds(197, 200, 97, 25);
		contentPane.add(btnSubmit);


	}
}
